package com.yyge.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyge.annotation.AdminAcces;
import com.yyge.dto.R;
import com.yyge.dto.ResultCode;
import com.yyge.entity.MerchandiseClass;
import com.yyge.service.MerchandiseClassService;
import com.yyge.util.TokenUtil;
import com.yyge.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

@Api("商品类型API")
@RestController
public class MerchandiseclassController extends BaseController {
    @Autowired
    MerchandiseClassService service;

    @Autowired
    TokenUtil tokenUtil;
    @GetMapping("/merchandiseclass/data")
    public R getMerchandiseclass(){
        QueryWrapper warpper=new QueryWrapper<>();
        PageVo pageVo =this.page(1,10,pageInfo ->service.page(pageInfo));

        return R.ok(pageVo);
    }

    @GetMapping("/merchandiseclass/{id}")
    public R getClass(@PathVariable("id") String id){
        MerchandiseClass merchandiseClass=new MerchandiseClass();
        merchandiseClass.setId(Integer.parseInt(id));
        QueryWrapper warpper=new QueryWrapper<>(merchandiseClass);
        return R.ok(service.list(warpper));
    }
    @DeleteMapping("/merchandiseclass/{id}")
    public R deleteClass(@PathVariable("id") Integer[] list){
        return R.ok(service.removeByIds(Arrays.asList(list)));
    }

//    @DeleteMapping("/merchandiseclass/{list}")
//    public R deleteBatchClass(@PathVariable("list") Integer[] str){
//        System.out.println("=====================");
//        System.out.println(str);
//        return null;
//    }

    @PutMapping("/merchandiseclass")
    public R PutClass(@RequestBody MerchandiseClass merchandiseClass){
        return R.ok(service.saveOrUpdate(merchandiseClass));
    }


    @PostMapping("/merchandiseclass")
    @AdminAcces("jack")
    public R addClass(@RequestBody MerchandiseClass merchandiseClass){
        merchandiseClass.setCreateTime(new Date());
        merchandiseClass.setUpdateTime(new Date());
        String name = tokenUtil.loginVOThreadLocal.get().getName();
        merchandiseClass.setUpdateBy(name);
        merchandiseClass.setCreateBy(name);
        System.out.println(merchandiseClass);
        boolean save =service.save(merchandiseClass);
        return save==true?R.ok("新增成功!"):R.error(ResultCode.NOT_FOUND);
    }

}
